<?php


namespace app\shop\controller\pc;


use app\BaseController;
use app\shop\model\config\ArticleModel;
use think\facade\Cache;

class ArticleController extends BaseController
{
    /**
     * 公告列表
     * @return \think\response\Json
     */
    public function getArticleList()
    {
        try {
            $page = $this->request->param('page', 1, 'int');
            $limit = $this->request->param('limit', 10, 'int');
            $uid = $this->request->middleware('userId');
            $list = ArticleModel::field('id,title,create_time,content')
                ->order('id', 'desc')
                ->paginate(['list_rows' => $limit, 'page' => $page])
                ->toArray();
            foreach ($list['data'] as &$value) {
                $value['create_time'] = date('Y年m月d日 H:i:s', strtotime($value['create_time']));
                $value['create_timestamp'] = intval(strtotime($value['create_time']) * 1000);
                $value['is_read'] = $this->isReadNotice($value['id'], $uid);
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 公告详情
     * @return \think\response\Json
     */
    public function getArticleDetail()
    {
        try {
            $id = $this->request->param('id');
            $uid = $this->request->middleware('userId');
            $info = ArticleModel::field('id,title,create_time,content')->where('id', $id)->find();
            $this->readNotice($id, $uid);
            return $this->success($info);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 判断公告是否已被指定人读取
     * @param $articleId
     * @param $uid
     * @return bool
     */
    public function isReadNotice($articleId, $uid)
    {
        $key = sprintf('article_%s', $articleId);
        return Cache::store('redis')->handler()->SISMEMBER($key, $uid);
    }

    /**
     * 已读群公告
     * @param $articleId
     * @param $uid
     * @return void
     */
    public function readNotice($articleId, $uid)
    {
        $key = sprintf('article_%s', $articleId);
        Cache::store('redis')->handler()->SADD($key, $uid);
    }

    /**
     *获取最新公告信息
     * @return \think\response\Json
     */
    public function getLastArticleInfo()
    {
        try {
            $uid = $this->request->middleware('userId');

            $content = '';
            $time = time();
            $noRead = 0;
            $list = ArticleModel::field('id,title,create_time,content')->select();
            foreach ($list as $value) {
                if (!$this->isReadNotice($value['id'], $uid)) {
                    $noRead += 1;
                }
                $content = $value['content'];
                $time = $value->getData('create_time');
            }
            $info = [
                'no_read' => $noRead,
                'content' => $content,
                'time' => $time,
            ];
            return $this->success($info);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}